﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Xml;


namespace FRL.DebtChase.Common
{

	/// <summary>
	/// Declaration of CommBodyWrapper class
	/// </summary>
	public class CommBodyWrapper
	{

		/////////////////////////////////////////////////////////////
		// Constants

		public const string ElementName = "CommBody";
		public const string SectionElementName = "Section";

		/////////////////////////////////////////////////////////////
		// Attributes

		private int m_iCommBodyId = 0;
		public int CommBodyId
		{
			get { return m_iCommBodyId; }
			set { m_iCommBodyId = value; }
		}

		private List<string> m_strSections = new List<string>();
		public List<string> Sections
		{
			get { return m_strSections; }
		}

		public bool IsEmpty
		{
			get { return (m_iCommBodyId == 0); }
		}

		/////////////////////////////////////////////////////////////
		// Serialization

		public void Write(XmlWriter w, string strTag)
		{

			// Write element
			w.WriteStartElement(ElementName);
			{

				// Write attributes
				if (strTag != null)
					w.WriteAttributeString("tag", strTag);
				w.WriteAttributeString("commBodyId", m_iCommBodyId.ToString());

				// Write elements
				foreach (string strSection in m_strSections)
				{
					w.WriteStartElement(SectionElementName);
					{
						w.WriteAttributeString("name", strSection);
					}
					w.WriteEndElement();
				}

			}
			w.WriteEndElement();

		}

		public void Read(XmlReader r)
		{
#if DEBUG
			Debug.Assert(r.Name == ElementName);
#endif // #if DEBUG

			// Read attributes
			m_iCommBodyId = r.SafeReadIntAttribute("commBodyId",0);

			// If there are child elements...
			m_strSections.Clear();
			if (!r.IsEmptyElement)
			{

				// Loop through elements
				while (r.Read())
				{

					// If this is a workflow action...
					if (r.NodeType == XmlNodeType.Element)
					{

						// Compare names
						if (r.Name == SectionElementName)
						{
							string strName = r.GetAttribute("name");
							if (!string.IsNullOrEmpty(strName))
								m_strSections.Add(strName);
#if DEBUG
							else
								Debug.Assert(false);
#endif // #if DEBUG
						}
#if DEBUG
						else
							Debug.Assert(false);
#endif // #if DEBUG
					}
					else
						break;

				} // while-loop

			}

		}

	}

}
